package com.example.tests;

import static org.hamcrest.Matchers.equalTo;
import com.example.utils.ListOf;
import static org.hamcrest.Matchers.*;
import static org.hamcrest.MatcherAssert.*;
import org.testng.annotations.Test;

public class FolderCreationTests extends TestBase {
	
	@Test
	public void testFolderCreation() {
		String folder = "newfolder";
		ListOf<String> oldList = app.getFolderHelper().getFolders();
		app.getFolderHelper().createFolder(folder);
		ListOf<String> newList = app.getFolderHelper().getFolders();
		assertThat(newList, equalTo(oldList.withAppended(folder)));
	}
	
	@Test
	public void testVariousFoldersCreation() {
		String folder1 = "newfolder1";
		String folder2 = "newfolder2";
		ListOf<String> oldList = app.getFolderHelper().getFolders();
		assertThat(app.getFolderHelper().createFolder(folder1), is(nullValue()));
		ListOf<String> newList = app.getFolderHelper().getFolders();
		assertThat(newList, equalTo(oldList.withAppended(folder1)));
		// variable oldList will point to newList's memory area 
		oldList = newList;
		assertThat(app.getFolderHelper().createFolder(folder2), is(nullValue()));
		// variable newList points to new memory area  
		newList = app.getFolderHelper().getFolders();
		assertThat(newList, equalTo(oldList.withAppended(folder2)));
	}
	
	@Test
	public void testFoldersWithSameNameCreation() {
		String folder1 = "newfolder3";
		String folder2 = "newfolder3";
		ListOf<String> oldList = app.getFolderHelper().getFolders();
		assertThat(app.getFolderHelper().createFolder(folder1), is(nullValue()));
		ListOf<String> newList = app.getFolderHelper().getFolders();
		assertThat(newList, equalTo(oldList.withAppended(folder1)));
		
		oldList = newList;
		assertThat(app.getFolderHelper().createFolder(folder2), containsString("Duplicated folder name"));
		newList = app.getFolderHelper().getFolders();
		assertThat(newList, equalTo(oldList));
	}
}
